***************
*** Figure 1
***************
global db "D:\Dropbox\unequal_gains\main_data"
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"

*** Panels A & B ***

clear
set obs 1 
generate ces_price_index=.
save "$db/Important Price Datasets/inflation_income.dta", replace

foreach i of numlist 2004(1)2014 {

local var=`i'+1

use "$db/Important Price Datasets/price_index_household_income_`i'_2004_modules_final.dta", clear
rename total_quantity total_quantity_old
rename average_unit_price average_unit_price_old
rename average_unit_price_all average_unit_price_all_old

merge 1:1 upc upc_ver income_quintile using "$db/Important Price Datasets/price_index_household_income_`var'_2004_modules_final.dta"
keep if _merge==3
drop total_spending

gen double price_ratio=average_unit_price/average_unit_price_old
gen double price_ratio_all=average_unit_price_all/average_unit_price_all_old
* get rid of outliers
sum price_ratio, d
drop if price_ratio<r(p1) | price_ratio>r(p99)
sum price_ratio_all, d
drop if price_ratio_all<r(p1) | price_ratio_all>r(p99)

** Part 1: standard price indices with quintile-specific unit prices **

* compute UPC spending/shares by quintiles: 
bysort income_quintile: egen double total_spending=sum(average_unit_price*total_quantity)
bysort income_quintile: egen double total_spending_old=sum(average_unit_price_old*total_quantity_old)
gen double share=average_unit_price*total_quantity/total_spending
gen double share_old=average_unit_price_old*total_quantity_old/total_spending_old

* CES
gen double weight_num=(share-share_old)/(ln(share)-ln(share_old))
bysort income_quintile: egen double weight_den=sum(weight_num)
gen double weight=weight_num/weight_den
gen double numerator_temp=weight*log(price_ratio)
bysort income_quintile: egen double numerator=sum(numerator_temp)
gen double ces_price_index=exp(numerator)
drop numerator numerator_temp weight*

* Laspeyres
gen double numerator_temp=average_unit_price*total_quantity_old 
gen double denominator_temp=average_unit_price_old*total_quantity_old
bysort income_quintile: egen double numerator=sum(numerator_temp)
bysort income_quintile: egen double denominator=sum(denominator_temp)
gen laspeyres_price_index=numerator/denominator
drop numerator denominator numerator_temp denominator_temp

* Paasche
gen double numerator_temp=average_unit_price*total_quantity
gen double denominator_temp=average_unit_price_old*total_quantity
bysort income_quintile: egen double numerator=sum(numerator_temp)
bysort income_quintile: egen double denominator=sum(denominator_temp)
gen paasche_price_index=numerator/denominator
drop numerator denominator numerator_temp denominator_temp

* Tornqvist 
gen weight=1/2*(share+share_old)
gen double numerator_temp=weight*log(price_ratio)
bysort income_quintile: egen double numerator=sum(numerator_temp)
gen double tornqvist_price_index=exp(numerator)
drop numerator numerator_temp weight

* Fisher
gen fisher_price_index=sqrt(paasche_price_index*laspeyres_price_index)

* Geometric Paasche 
gen weight=share
gen double numerator_temp=weight*log(price_ratio)
bysort income_quintile: egen double numerator=sum(numerator_temp)
bysort income_quintile: egen double denominator=sum(weight)
gen double gpaasche_price_index=exp(numerator/denominator)
drop weight numerator_temp numerator denominator

* Geometric Laspeyres 
gen weight=share_old
gen double numerator_temp=weight*log(price_ratio)
bysort income_quintile: egen double numerator=sum(numerator_temp)
bysort income_quintile: egen double denominator=sum(weight)
gen double glaspeyres_price_index=exp(numerator/denominator)
drop weight numerator_temp numerator denominator

* Walsh 
gen double numerator_temp=average_unit_price*sqrt(total_quantity*total_quantity_old)
gen double denominator_temp=average_unit_price_old*sqrt(total_quantity*total_quantity_old)
bysort income_quintile: egen double numerator=sum(numerator_temp)
bysort income_quintile: egen double denominator=sum(denominator_temp)
gen walsh_price_index=numerator/denominator
drop numerator* denominator*

* Marshall-Edgeworth
gen double numerator_temp=average_unit_price*(total_quantity+total_quantity_old)
gen double denominator_temp=average_unit_price_old*(total_quantity+total_quantity_old)
bysort income_quintile: egen double numerator=sum(numerator_temp)
bysort income_quintile: egen double denominator=sum(denominator_temp)
gen ME_price_index=numerator/denominator
drop numerator* denominator*

** Part 2: Tornqvist inflation "across UPCs" (using average unit prices) **

* Across UPCs 
gen weight=1/2*(share+share_old)
gen double numerator_temp=weight*log(price_ratio_all)
bysort income_quintile: egen double numerator=sum(numerator_temp)
gen double tornqvist_all_price_index=exp(numerator)
drop weight numerator numerator_temp

** Part 3: compute Nested CES 
* (nests: modules within groups within departments)

* NCES step 1: module level
drop total_spending*
bysort product_module_code income_quintile: egen double total_spendingM=sum(average_unit_price*total_quantity)
bysort product_module_code income_quintile: egen double total_spendingM_old=sum(average_unit_price_old*total_quantity_old)
gen double shareM=average_unit_price*total_quantity/total_spendingM
gen double shareM_old=average_unit_price_old*total_quantity_old/total_spendingM_old

bysort product_module_code income_quintile: gen double weightM_num=(shareM-shareM_old)/(ln(shareM)-ln(shareM_old))
bysort product_module_code income_quintile: egen double weightM_den=sum(weightM_num)
gen double weightM=weightM_num/weightM_den

gen double numeratorM_temp=weightM*log(price_ratio)
bysort product_module_code income_quintile: egen double numeratorM=sum(numeratorM_temp)
gen double nces_price_index=exp(numeratorM)

* NCES step 2: product group level
keep tornqvist_price_index ces_price_index laspeyres_price_index paasche_price_index income_quintile nces_price_index ///
product_module_code product_group_code department_code tornqvist_all_price_index ///
total_spendingM total_spendingM_old
duplicates drop 

bysort product_group_code income_quintile: egen double total_spending_group=sum(total_spendingM)
bysort product_group_code income_quintile: egen double total_spending_group_old=sum(total_spendingM_old)
gen double share=total_spendingM/total_spending_group
gen double share_old=total_spendingM_old/total_spending_group_old

bysort product_group_code income_quintile: gen double weight_num=(share-share_old)/(ln(share)-ln(share_old))
bysort product_group_code income_quintile: egen double weight_den=sum(weight_num)
gen double weight=weight_num/weight_den

gen double numerator_temp=weight*log(nces_price_index)
bysort product_group_code income_quintile: egen double numerator=sum(numerator_temp)
gen double nces_price_index_group=exp(numerator)

* NCES step 3: department level
keep tornqvist_price_index ces_price_index laspeyres_price_index paasche_price_index income_quintile nces_price_index_group ///
product_group_code department_code tornqvist_all_price_index ///
total_spending_group total_spending_group_old
duplicates drop 

bysort department_code income_quintile: egen double total_spending_department=sum(total_spending_group)
bysort department_code income_quintile: egen double total_spending_department_old=sum(total_spending_group_old)
gen double share=total_spending_group/total_spending_department
gen double share_old=total_spending_group_old/total_spending_department_old

bysort department_code income_quintile: gen double weight_num=(share-share_old)/(ln(share)-ln(share_old))
bysort department_code income_quintile: egen double weight_den=sum(weight_num)
gen double weight=weight_num/weight_den

gen double numerator_temp=weight*log(nces_price_index_group)
bysort department_code income_quintile: egen double numerator=sum(numerator_temp)
gen double nces_price_index_dep=exp(numerator)

keep tornqvist_price_index ces_price_index laspeyres_price_index paasche_price_index income_quintile nces_price_index_dep ///
department_code tornqvist_all_price_index ///
total_spending_department total_spending_department_old
duplicates drop 

* NCES step 4: overall level
bysort income_quintile: egen double total_spending_full=sum(total_spending_department)
bysort income_quintile: egen double total_spending_full_old=sum(total_spending_department_old)
gen double share=total_spending_department/total_spending_full
gen double share_old=total_spending_department_old/total_spending_full_old

gen double weight_num=(share-share_old)/(ln(share)-ln(share_old))
bysort income_quintile: egen double weight_den=sum(weight_num)
gen double weight=weight_num/weight_den

gen double numerator_temp=weight*log(nces_price_index_dep)
bysort income_quintile: egen double numerator=sum(numerator_temp)
gen double nces_price_index_full=exp(numerator)

keep tornqvist_price_index ces_price_index laspeyres_price_index paasche_price_index income_quintile nces_price_index_full ///
tornqvist_all_price_index
duplicates drop 

gen year=`var'

append using "$db/Important Price Datasets/inflation_income.dta"
drop if missing(nces_price_index)
save "$db/Important Price Datasets/inflation_income.dta", replace 
}

* Panel A
use "$db/Important Price Datasets/inflation_income.dta", clear
foreach i in ces laspeyres paasche tornqvist nces {
gen `i'_infl=(`i'_price-1)*100
}
collapse (mean) *_infl, by(income_quintile)

scatter tornq income_quintile || lfit tornq inc, lcolor(maroon) graphregion(color(white)) ///
 xtitle("Household Income Quintile") ytitle("Tornqvist Annual Inflation Rate," "Avg. 2004-2015, %") ///
 leg(off) ylabel(1(0.2)2,angle(0))
graph export "$resultspath/Figure1PanelA.pdf", as(pdf) replace
graph save "$resultspath/Figure1PanelA.gph", replace

* Panel B
keep if income_quintile==1 | income_quintile==5
gsort - income_quintile
foreach i in ces laspeyres paasche tornqvist nces {
gen `i'_infl_diff=`i'_infl-`i'_infl[_n-1]
tab `i'_infl_diff
}
keep *_infl_diff
rename tornqvist index1
rename laspeyres index2
rename paasche index3
rename ces index4
rename nces index5
drop if missing(index1)
gen id=1
reshape long index, i(id) j(price_index)

scatter index price_index, ///
xlabel(1 "Tornqvist" 2 "Laspeyres" 3 "Paasche" 4 "CES" 5 `""Nested" "CES""') ylabel(0.50(0.05)0.80, angle(0)) ///
xtitle("") ytitle("Annual Inflation Difference b/w Bottom" "and Top Income Quintiles, Avg. 2004-2015, pp") ///
graphregion(color(white))
graph export "$resultspath/Figure1PanelB.pdf", as(pdf) replace
graph save "$resultspath/Figure1PanelB.gph", replace

*** Panel C ***

clear
set obs 1 
generate tornqvist_price_index=.
save "$db/Important Price Datasets/inflation_age_income.dta", replace

foreach i of numlist 2004(1)2014 {

local var=`i'+1

use "$db/Important Price Datasets/price_index_household_income_`i'_2004_modules_final_age.dta", clear
rename total_quantity total_quantity_old
rename average_unit_price average_unit_price_old
rename average_unit_price_all average_unit_price_all_old

merge 1:1 upc upc_ver income_quintile age_group using "$db/Important Price Datasets/price_index_household_income_`var'_2004_modules_final_age.dta"
keep if _merge==3
drop total_spending

gen double price_ratio=average_unit_price/average_unit_price_old
gen double price_ratio_all=average_unit_price_all/average_unit_price_all_old
* get rid of outliers
sum price_ratio, d
drop if price_ratio<r(p1) | price_ratio>r(p99)
sum price_ratio_all, d
drop if price_ratio_all<r(p1) | price_ratio_all>r(p99)

** Part 1: standard price indices with quintile-specific unit prices **

* compute UPC spending/shares by quintiles by age groups: 
bysort income_quintile age_group: egen double total_spending=sum(average_unit_price*total_quantity)
bysort income_quintile age_group: egen double total_spending_old=sum(average_unit_price_old*total_quantity_old)
gen double share=average_unit_price*total_quantity/total_spending
gen double share_old=average_unit_price_old*total_quantity_old/total_spending_old

* Tornqvist 
gen weight=1/2*(share+share_old)
gen double numerator_temp=weight*log(price_ratio)
bysort income_quintile age_group: egen double numerator=sum(numerator_temp)
gen double tornqvist_price_index=exp(numerator)
drop numerator numerator_temp weight

keep tornqvist_price_index income_quintile age_group
duplicates drop 

gen year=`var'

append using "$db/Important Price Datasets/inflation_age_income.dta"
drop if missing(tornqvist)
save "$db/Important Price Datasets/inflation_age_income.dta", replace 
}

use "$db/Important Price Datasets/inflation_age_income.dta", clear
* we drop age groups<25
drop if missing(age_group)
foreach i in tornqvist {
gen `i'_infl=(`i'_price-1)*100
}
collapse (mean) *_infl, by(income_quintile age_group)

graph twoway (bar tornqvist_infl income_quintile, xlabel(1(1)5) ylabel(0(0.5)2)), ///
by(age_group) graphregion(color(white))  ///
plotregion(fcolor(white)) graphregion(fcolor(white)) ///
ytitle("Tornqvist Annual Inflation Rate," "Avg. 2004-2015, %") ///
ylabel(, angle(horizontal)) xtitle("Household Income Quintiles by Age Groups")
graph save "$resultspath/Figure1PanelC.gph", replace


